<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('商品列表')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="post-form">
					<div class="select-list">
						<ul>
							<li>
								商品编码：<input type="text" name="product_id"/>
							</li>
							<li>
								商品名称：<input type="text" name="product_name"/>
							</li>
							<li>
								商品状态：<select name="state" >
									<option value="">所有</option>
									<option text="" value="1">上架</option>
									<option text="" value="0">下架</option>
								</select>
							</li>
							<li>
								<label> 分类：</label>
								<select name="sort" >
									<option value="">所有</option>
									<option th:each="sort :${sl}" th:text="${sort.sort_name}" th:value="${sort.id}"></option>
								</select>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="searchPre()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							    <a class="btn btn-warning btn-rounded btn-sm" onclick="resetPre()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			<div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:water:add">
				<i class="fa fa-plus"></i> 新增
			</a>
				<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:water:update">
					<i class="fa fa-edit"></i> 修改
				</a>
	        </div>
	        
	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
		var manageFlag = [[${@permission.hasPermi('system:admin:manage')}]];
		var removeFlag = [[${@permission.hasPermi('system:water:view')}]];
		var editFlag = [[${@permission.hasPermi('water:product:edit')}]];
		var datas = [[${@dict.getType('sys_normal_disable')}]];
		var prefix = ctx + "water";
		$(function() {
		    var options = {
		        url:  prefix+ "/list",
		        createUrl: prefix + "/add",
		        updateUrl: prefix + "/edit/{id}",
		        removeUrl: prefix + "/removeProduct",
		        exportUrl: prefix + "/export",
		        modalName: "商品列表",
		        columns: [{
		            checkbox: true
		        },
		        {
		            field: 'id',
		            title: '商品编号'
		        },
		        {
		            field: 'product_name',
		            title: '商品名称',
		        },
					{
						title: '商品分类',
							//查询数据库，找到sort_id对应的分类名
						formatter: function(value,row,index){
							var actions = [];
							actions.push('<span >'+selectSortBySort_id(row)+'</span>&nbsp;');
							return actions.join('');
						}
					},
					{
						field: 'info',
						title: '商品信息',
					},
					{
						field: 'productState',
						title: '主商品状态',
						formatter: function(value, row, index) {
							var actions = [];
							if(value == 1){
								actions.push('<span style="color: darkseagreen">上架</span>&nbsp;');
							}else if (value == 0){
								actions.push('<span style="color: orangered">下架</span>&nbsp;');
							}else {
								actions.push('<span style="color: orangered">异常</span>&nbsp;');
							}
							return actions.join('');
						}
					},
					{
						visible: editFlag == 'hidden' ? false : true,
						// title: '状态',
						align: 'center',
						formatter: function (value, row, index) {
							return statusTools(row);
						}
					},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		                var actions = [];
						actions.push("<a class='btn btn-primary btn-xs " + manageFlag + "' href='javascript:void(0)' onclick='manage(" + row.id + ")'><i class='fa fa-key'></i>子商品管理</a> ");
						actions.push('<a class="btn btn-success btn-xs" href="#" onclick="editImg('+row.id+')"><i class="fa fa-edit"></i>编辑</a> ');
						actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id+'\')"><i class="fa fa-remove"></i>删除</a>&nbsp;');
						return actions.join('');
		            }
		        }]
		    };
		    $.table.init(options);
		});
		/* 管理进度 */
		function manage(id) {
			var url = prefix + '/showChildren/' + id;
			//第一个参数是标签的名称
			$.modal.openTab("子商品管理", url);
		}
		function searchPre() {
			var data = {};

			$.table.search('post-form', 'bootstrap-table', data);
		}
		function resetPre() {
			$("#post-form")[0].reset();
			$.table.search('post-form', 'bootstrap-table');
		}
		function selectSortBySort_id(row){
			var message ='' ;
			var id = row.sort_id;
			$.ajax({
				url:prefix+"/selectSortBySort_id/"+id,
				type: "post",
				dataType:"json",
				async:false,     //关闭异步，同步传输
				success :function (data) {
					var actions = [];
					message = data.sortEntity.sort_name;
					console.log(message);
				}
			});
			return message;
		}
		/* 商品上架 */
		function statusTools(row) {
			if (row.productState == 0) {
				return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.id + '\')"></i> ';
			} else {
				return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.id + '\')"></i> ';
			}
		}
		/* 商品下架 */
		function disable(id) {
			$.modal.confirm("确认要下架商品吗？", function() {
				$.operate.post(prefix + "/changeProductStatus", { "id": id, "productState": 0 });
			})
		}

		/* 商品上架 */
		function enable(id) {
			$.modal.confirm("确认要上架商品吗？", function() {
				$.operate.post(prefix + "/changeProductStatus", { "id": id, "productState": 1 });
			})
		}

		function  editImg(id) {
			var url = prefix + '/editImg/' + id;
			//第一个参数是标签的名称
			// $.modal.openTab("商品详情图管理", url);
			$.modal.open('商品详情图',url, '1000', '800');
		}
	</script>
</body>
</html>